cmake_minimum_required (VERSION 3.8)

set(PROJECT_NAME "download")
project(${PROJECT_NAME} VERSION 1.0)

set(CMAKE_CXX_STANDARD 14)
if(NOT DEFINED CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE "Release")
endif()

# DEBIAN_VERSION
execute_process(COMMAND date "+%F %T%z" OUTPUT_VARIABLE TIMESTAMP OUTPUT_STRIP_TRAILING_WHITESPACE) 
execute_process(COMMAND date "+%m%d" OUTPUT_VARIABLE MMDD OUTPUT_STRIP_TRAILING_WHITESPACE) 
set(DEBIAN_VERSION ${PROJECT_VERSION}.${MMDD}-001)


## Compile and link options
add_compile_options(
  -pthread
  -Werror
  -Wswitch
  -Wuninitialized
  -Wreturn-type
)

####################
## 2.Find Package ##
####################

find_package(CURL REQUIRED)
find_package(OpenSSL REQUIRED)



#####################
## 3.Build & Check ##
#####################
# Include
include_directories(
  include
  /usr/include
  /usr/local/include
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

## Source files
file(GLOB_RECURSE ALL_SRCS "src/*.cpp")
file(GLOB_RECURSE EXAMPLE_SRCS "src/examples/*.cpp")
file(GLOB_RECURSE TEST_SRCS "src/tests/*.cpp")

list(LENGTH EXAMPLE_SRCS EXAMPLE_SRCS_LENGTH)
if (${EXAMPLE_SRCS_LENGTH} GREATER 0)
  list(REMOVE_ITEM ALL_SRCS ${EXAMPLE_SRCS})
endif()

list(LENGTH TEST_SRCS TEST_SRCS_LENGTH)
if (${TEST_SRCS_LENGTH} GREATER 0)
  list(REMOVE_ITEM ALL_SRCS ${TEST_SRCS})
endif()


set(${PROJECT_NAME}_SOURCES ${ALL_SRCS})

# 全局链接动态库

## LIBS
set(${PROJECT_NAME}_LIBS
  curl
  crypto
)


add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_SOURCES})

target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_LIBS})




#############
## Package ##
#############
set(DEBIAN_CONTROL_PACKAGE ${PROJECT_NAME})
set(DEBIAN_CONTROL_MAINTAINER ztq)
set(DEBIAN_CONTROL_DEPENDS )
set(DEBIAN_CONTROL_SECTION ${PROJECT_NAME})
set(DEBIAN_CONTROL_DESCRIPTION ${PROJECT_NAME})
set(          DEBIAN_CONTROL "Package: ${DEBIAN_CONTROL_PACKAGE}\\n")
string(APPEND DEBIAN_CONTROL "Version: 1.0\\n")
string(APPEND DEBIAN_CONTROL "Architecture: amd64\\n")
string(APPEND DEBIAN_CONTROL "Maintainer: ${DEBIAN_CONTROL_MAINTAINER}\\n")
string(APPEND DEBIAN_CONTROL "Depends: ${DEBIAN_CONTROL_DEPENDS}\\n")
string(APPEND DEBIAN_CONTROL "Section: ${DEBIAN_CONTROL_SECTION}\\n")
string(APPEND DEBIAN_CONTROL "Priority: optional\\n")
string(APPEND DEBIAN_CONTROL "Homepage: \\n")
string(APPEND DEBIAN_CONTROL "Description: ${DEBIAN_CONTROL_DESCRIPTION}")

set(DEBIAN_CONTROL_PACKAGE_NAME ${DEBIAN_CONTROL_PACKAGE}-${DEBIAN_VERSION})
set(PACKAGE_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/${DEBIAN_CONTROL_PACKAGE_NAME}/usr/local)

add_custom_target(pack
  COMMAND echo "CMAKE_CURRENT_BINARY_DIR:" ${CMAKE_CURRENT_BINARY_DIR}
  COMMAND echo "CMAKE_CURRENT_SOURCE_DIR:" ${CMAKE_CURRENT_SOURCE_DIR}
  COMMAND cd ${CMAKE_CURRENT_BINARY_DIR} && rm -rf ${DEBIAN_CONTROL_PACKAGE_NAME} && mkdir -p ${DEBIAN_CONTROL_PACKAGE_NAME}/DEBIAN
  COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/${DEBIAN_CONTROL_PACKAGE_NAME}/DEBIAN && echo \"${DEBIAN_CONTROL}\" | sed 's|\\\\ | |g' >control
  COMMAND cd ${CMAKE_CURRENT_BINARY_DIR} && mkdir -p ${PACKAGE_INSTALL_DIR}/sbin
  COMMAND cd ${PACKAGE_INSTALL_DIR} && cp ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} sbin && chmod +x sbin/${PROJECT_NAME} 
  COMMAND cd ${CMAKE_CURRENT_BINARY_DIR} && dpkg -b ${DEBIAN_CONTROL_PACKAGE_NAME} ${DEBIAN_CONTROL_PACKAGE_NAME}.deb
  COMMAND cd ${PACKAGE_INSTALL_DIR} && mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/deb && cp ${CMAKE_CURRENT_BINARY_DIR}/${DEBIAN_CONTROL_PACKAGE_NAME}.deb ${CMAKE_CURRENT_SOURCE_DIR}/deb
  COMMAND echo "package success"
  COMMENT "package ${DEBIAN_CONTROL_PACKAGE} deb"
  DEPENDS download
)

